/**
 * 
 */
package co.edu.eam.iso.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import co.edu.eam.iso.enums.SubCharacteristicEnum;
import co.edu.eam.iso.evaluator.Metric;
import co.edu.eam.iso.logger.Logger;

/**
 * @author Giovanny Ramirez Blandon
 * @version 1.0
 */
public class MetricDAO {
	
	public Metric findMetric(int id) {
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Metric metric = null;
		try {
			preparedStatement = DataSource.getConnection().prepareStatement(
					"select m.id, m.description, m.id_subcharacteristic from metrics m where m.id =?");
			preparedStatement.setInt(1, id);

			resultSet = preparedStatement.executeQuery();
			
			while (resultSet.next()) {
				metric = new Metric();
				metric.setId(resultSet.getInt(1));
				metric.setDescription(resultSet.getString(2));
				metric.setSubCharacteristicEnum(SubCharacteristicEnum.getById(resultSet.getInt(3)));
			}
		} catch (SQLException e) {
			Logger.getInstance().registrarLog(e);
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				
				if (resultSet != null) {
					resultSet.close();
				}				
				
				DataSource.getConnection().close();
			} catch (SQLException e) {
				Logger.getInstance().registrarLog(e);
			}
		}
		
		return metric;
	}
	
	public void updateMetric(int id, String description, int idSubcharacteristic) {
		PreparedStatement preparedStatement = null;
		
		try {
			preparedStatement = DataSource.getConnection().prepareStatement(
					"update metrics set description =?, id_subcharacteristic =? where id =?");
			preparedStatement.setString(1, description);
			preparedStatement.setInt(2, idSubcharacteristic);
			preparedStatement.setInt(3, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			Logger.getInstance().registrarLog(e);
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				
				DataSource.getConnection().close();
			} catch (SQLException e) {
				Logger.getInstance().registrarLog(e);
			}
		}
	}
	
	public void deleteMetric(int id) {
		PreparedStatement preparedStatement = null;
		
		try {
			preparedStatement = DataSource.getConnection().prepareStatement(
					"delete from metrics where id =?");
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			Logger.getInstance().registrarLog(e);
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				
				DataSource.getConnection().close();
			} catch (SQLException e) {
				Logger.getInstance().registrarLog(e);
			}
		}
	}

	public void createMetric(String description, int idSubcharacteristic) {
		PreparedStatement preparedStatement = null;
		
		try {
			preparedStatement = DataSource.getConnection().prepareStatement(
					"insert into metrics (description, id_subcharacteristic) values (?, ?);");
			preparedStatement.setString(1, description);
			preparedStatement.setInt(2, idSubcharacteristic);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			Logger.getInstance().registrarLog(e);
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				
				DataSource.getConnection().close();
			} catch (SQLException e) {
				Logger.getInstance().registrarLog(e);
			}
		}
	}
	
	public List<Metric> searchMetrics(int idSubcharacteristic) {
		List<Metric> metrics = new ArrayList<Metric>();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		
		try {
			preparedStatement = DataSource.getConnection().prepareStatement(
							"select m.id, m.description from metrics m where m.id_subcharacteristic =?;");
			preparedStatement.setInt(1, idSubcharacteristic);
			resultSet = preparedStatement.executeQuery();

			while (resultSet.next()) {
				Metric metric = new Metric();
				metric.setId(resultSet.getInt(1));
				metric.setDescription(resultSet.getString(2));
				metrics.add(metric);
			}
		} catch (SQLException e) {
			Logger.getInstance().registrarLog(e);
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				
				if (resultSet != null) {
					resultSet.close();
				}
				
				DataSource.getConnection().close();
			} catch (SQLException e) {
				Logger.getInstance().registrarLog(e);
			}
		}
		
		return metrics;
	}

}
